<?php
/**
 * Created by PhpStorm.
 * User: allen
 * Date: 2019/3/8
 * Time: 10:38
 */
namespace app\index\controller;

use app\index\model\Mch;
use think\App;

class User extends Base {

    public function __construct(App $app = null){
        parent::__construct($app);
    }

    /**
     * 商户申请
     * @param string $name
     * @param string $tel
     * @param string $company
     */
    public function mch($name='',$tel='',$company='',$proxy_id=''){
        $model = new Mch();
        $password = self::_password('123456');
        $result = $model->addNew(self::$uid,$name,$tel,$password,$company,$proxy_id,$out);
        if(!$result){
            return $this->__json(1,$out);
        }
        return $this->__json(0,'申请成功');
    }

    /**
     * 修改密码
     * @param $mc_id
     * @param string $password
     * @param string $password1
     */
    public function reset($mc_id=0,$password='',$password1=''){
        if(!$mc_id || !$password || !$password1){
            return $this->__json(1,'缺少参数');
        }
        if($password!=$password1){
            return $this->__json(1,'密码不一致');
        }
        $mch = (new Mch())->get($mc_id);
        if(!$mch){
            return $this->__json(1,'商户不存在');
        }
        $mch->password = self::_password($password);
        $mch->update_time= time();
        if(!$mch->save()){
            return $this->__json(1,'修改失败');
        }
        return $this->__json(0,'修改成功');
    }

    /**
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function mch_info(){
        $model = new Mch();
        $info = $model::field('id,name,company,tel,status,proxy_id,create_time')
            ->where('uid','eq',self::$uid)
            ->find();
        if(!$info){
            $this->__json(2,'done');
        }
        $this->__json(0,'done',$info);
    }

}